package com.zhang.sdk.ui.dialog.loading;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhang.sdk.R;

/**
 * loading dialog
 *
 * 功能：包括loading弹出、隐藏
 * @author zhang.x
 */
public class LoadingUIHelper {
    /** 加载数据对话框 */ // TODO: 2016/10/21 内存泄露原因？
    private Dialog mLoadingDialog;
    private View view;
    public AnimationDrawable adProgressSpinner;

    /**
     * 显示加载对话框
     * @param context 上下文
     * @param msg 对话框显示内容
     * @param cancelable 对话框是否可以取消
     */
    public void showDialogForLoading(Activity context, String msg, boolean cancelable) {
        try{
            if (mLoadingDialog == null || !mLoadingDialog.isShowing()) {
                view = View.inflate(context, R.layout.dialog_loading, null);
                TextView loadingText = view.findViewById(R.id.tv_dialog_msg);
                loadingText.setText(msg);
                mLoadingDialog = new Dialog(context, R.style.loading_dialog_style);
                mLoadingDialog.setCancelable(cancelable);
                mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                mLoadingDialog.show();
            } else {
                TextView loadingText = view.findViewById(R.id.tv_dialog_msg);
                loadingText.setText(msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 关闭加载对话框
     */
    public void hideDialogForLoading() {
        if(mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.cancel();
            mLoadingDialog = null;
            view = null;
        }
    }


}
